package GUI;

import static javax.media.opengl.GL.GL_COLOR_BUFFER_BIT;
import static javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT;
import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_MODELVIEW;
import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_PROJECTION;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.fixedfunc.GLLightingFunc;
import javax.media.opengl.glu.GLU;

public class GameRenderer implements GLEventListener
{
	public static Camera camera;
	
	private static GLU glu;

	public static float loc_angle_x = -90.0f;
	public static float loc_angle_y = 0.0f;
	public static float loc_angle_z = 0.0f;

	public static float offset_x = 0.0f;
	public static float offset_y = 0.0f;
	public static float offset_z = 0.0f;

	@Override
	public void init(GLAutoDrawable glDrawable) 
	{
		GL2 gl = glDrawable.getGL().getGL2();
		glu = new GLU();
		camera = new Camera();
		
		gl.glPolygonMode (GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
		gl.glShadeModel(GLLightingFunc.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepth(1.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthFunc(GL.GL_LEQUAL);
		gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
		//gl.glEnable(GLLightingFunc.GL_LIGHTING);
		//gl.glEnable(GLLightingFunc.GL_LIGHT0);

		gl.glFlush();
	}

	@Override
	public void display(GLAutoDrawable glDrawable) 
	{
		GL2 gl = glDrawable.getGL().getGL2();

		gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		camera.update(glu);
		
		scene(gl);

		gl.glFlush();
	}

	@Override
	public void reshape(GLAutoDrawable glDrawable, int x, int y, int width, int height) 
	{
		GL2 gl = glDrawable.getGL().getGL2();

		if (height == 0) height = 1;
		float aspect = (float)width / height;

		gl.glViewport(0, 0, width, height);

		gl.glMatrixMode(GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(45.0, aspect, 0.1, 100.0);

		gl.glMatrixMode(GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	@Override
	public void dispose(GLAutoDrawable glDrawable) 
	{

	}

	private void scene(GL2 gl)
	{
		//gl.glRotatef(loc_angle_z,0,1,0);
		//gl.glTranslated(offset_x,offset_y,offset_z);
		//gl.glTranslated(0,0,0);
		
		gl.glPushMatrix();
		scene_map(gl);
		gl.glBegin(GL2.GL_POLYGON);
		gl.glVertex3d (0, 0, 0);
		gl.glVertex3d (0, 1.5, 0);
		gl.glVertex3d (1.5, 1.5, 0);
		gl.glVertex3d (1.5, 0, 0);
		gl.glEnd();
		gl.glPopMatrix();
	}

	private void scene_map(GL2 gl)
	{
		float box_amount_x = 32f;
		float box_amount_z = 32f;

		float box_size_x = 1.5f;
		float box_size_z = 1.5f;
		
		float temp_x;
		float temp_z;

		for(float x = 0; x < box_amount_x; x++)
		{
			for(float z = 0; z < box_amount_z; z++)
			{
				temp_x = 0f + (x * box_size_x);
				temp_z = 0f + (z * box_size_z);

				gl.glBegin(GL2.GL_POLYGON);
				gl.glVertex3d (temp_x, 0, temp_z + box_size_z);
				gl.glVertex3d (temp_x, 0, temp_z);
				gl.glVertex3d (temp_x + box_size_x, 0, temp_z);
				gl.glVertex3d (temp_x + box_size_x, 0, temp_z + box_size_z);
				gl.glEnd();
			}
		}
	}
}
